#ifndef E555530B_66EC_432C_989A_8807A58CBB6C
#define E555530B_66EC_432C_989A_8807A58CBB6C

#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

const std::string protocol_sep = "\n";
const std::string blank_space_sep = " ";

// "9\n" + "123 + 456" + \n
void Encode(std::string& s)
{
	std::string ret = std::to_string(s.size());
	ret += protocol_sep;
	ret += s;
	s = ret + protocol_sep;
}

bool Decode(std::string& s,std::string* ret)
{
	// std::cout << s << std::endl;
	int pos = s.find(protocol_sep);
	if(pos == std::string::npos) return false;
	std::string len = s.substr(0,pos);
	int l = std::stoi(len);
	int total = len.size() + l + 2;
	// std::cout << l << std::endl;
	if(total > s.size()) return false;
	std::string content = s.substr(pos + 1,l);
	*ret = content;
	// std::cout << content << std::endl;
	s.erase(0,total);
	return true;
}

class Request
{
public:
	Request(int data1,int data2,char oper):x(data1),y(data2),op(oper)
	{}
	Request()
	{}
	~Request()
	{}
public:
	bool Serialize(std::string* out) // 序列化 + Encode
	{
#ifdef Myself
		std::string content = std::to_string(x);
		content += blank_space_sep;
		content += op;
		content += blank_space_sep;
		content += std::to_string(y);
		*out = content;
		return true;
#else
		Json::Value v;
		v["x"] = x;
		v["y"] = y;
		v["op"] = op;
		// Json::FastWriter w;
		Json::StyledWriter w;
		*out = w.write(v);
		return true;
#endif
	}
	// x op y
	bool Deserialize(const std::string& in)   //Decode + 反序列化
	{
#ifdef Myself
		int left = in.find(blank_space_sep);
		if(left == std::string::npos) return false;
		// std::cout << left << std::endl;
		x = std::stoi(in.substr(0,left));
		int right = in.rfind(blank_space_sep);
		if(right == std::string::npos) return false;
		if(right - 2 != left) return false;
		op = in[left + 1];
		// std::cout << right << std::endl;
		y = std::stoi(in.substr(right + 1));
		return true;
#else
		Json::Reader r;
		Json::Value v;
		r.parse(in,v);
		x = v["x"].asInt();
		y = v["y"].asInt();
		op = v["op"].asInt();
		return true;
#endif
	}
	void DebugPrint()
	{
		std::cout << x << " " << op << " " << y << std::endl;
	}
public:
	char op;
	int x;
	int y;
};

class Response{
public:
	Response(int r,int exit):result(r),code(exit)
	{}
	Response():code(0)
	{}
	~Response()
	{}
public:
		// 231 0
	bool Serialize(std::string* out) // 序列化 + Encode
	{
#ifdef Myself
		std::string content = std::to_string(result);
		content += blank_space_sep;
		*out = content + std::to_string(code);
		return true;
#else
		Json::Value v;
		v["result"] = result;
		v["code"] = code;
		// Json::FastWriter w;
		Json::StyledWriter w;
		*out = w.write(v);
		return true;
#endif
	}

	bool Deserialize(const std::string& in)   //Decode + 反序列化
	{
#ifdef Myself
		int pos = in.find(blank_space_sep);
		if(pos == std::string::npos) return false;
		result = std::stoi(in.substr(0,pos));
		code = std::stoi(in.substr(pos+1));
		return true;
#else
		Json::Value v;
		Json::Reader r;
		r.parse(in,v);
		result = v["result"].asInt();
		code = v["code"].asInt();
		return true;
#endif
	}
	void DebugPrint()
	{
		std::cout << result << " " << code << std::endl;
	}
public:
	int result;
	int code;
};
#endif /* E555530B_66EC_432C_989A_8807A58CBB6C */
